	function trim(str)
	{		
		for(var i = 0; i < str.length; i++)
		{
			if(str.charAt(i) != " ")
			{
				str = str.substring(i, str.length);
				break;
			}
		}
		for(var i = str.length - 1; i >=0; i--)
		{
			if(str.charAt(i) != " ")
			{
				str = str.substr(0, i + 1);
				break;
			}
		}
		return str;
	}
	
	function HTMLEncode(str)
	{
		var s = "";
		if (str.length == 0) return "";
		s = str.replace(/&/g, "&amp;");
		s = s.replace(/</g, "&lt;");
		s = s.replace(/>/g, "&gt;");
		s = s.replace(/ /g,"&nbsp;");
		s = s.replace(/\'/g, "&#39;");
		s = s.replace(/\"/g, "&quot;");		
		return s;  
	}

	function HTMLDecode(str)
	{
		var s = "";
		if (str.length == 0) return "";
		s = str.replace(/&amp;/g, "&");
		s = s.replace(/&lt;/g, "<");
		s = s.replace(/&gt;/g, ">");
		s = s.replace(/&nbsp;/g," ");
		s = s.replace(/&#39;/g, "\'");
		s = s.replace(/&quot;/g, "\"");		
		return s;  
	}	

	
	function str2number(str)
	{	
		return str.substr(0, str.length - 2);
	}	
	
	
 	function time2sec(time)//04:21.14,03:56
	{	
		if(time.match(/^\d+:\d+\.\d+$/) != null)	
		{
			return Number(time.substr(0, 2)) * 60 + Number(time.substr(3, 2));
		}	
		else if(time.match(/^\d+:\d+$/) != null)
		{
			return Number(time.substr(0, 2)) * 60 + Number(time.substr(3, 2));
		}
		return -1;
	}
	
	function timeCompare(a,b)
	{
		return a.second - b.second;
	}
		
	function lrc2json(lrc)
	{	
		//lrc = lrc.replace(/\]\n/g,"]");		
		var retJson = new Array();
		var lrcArray = lrc.split("\n");
		var pos = 0;
		for(var i = 0 ; i < lrcArray.length; i++)
		{	
			str = trim(lrcArray[i]);
			offset = str.lastIndexOf("]");
			content = HTMLEncode(str.substring(offset + 1, str.length));						
			timeArray = str.match(/\[\d+:\d+\.\d+]/g);			
			if(timeArray == null)
			{					
				timeArray = str.match(/\[\d+:\d+]/g);
			}
			if(timeArray == null || timeArray.length == 0 || content.length == 0)
			{
				continue;
			}
			for(var j = 0; j < timeArray.length; j++)
			{
				time = timeArray[j].substr(1, 5);
				sec = time2sec(time);
				retJson[pos] = eval("({'time':'" + time + 
									"','second':" +  sec + 
									",'content':'" +  content + 
									"'})");
				pos++;
			}
		}		
		
		retJson = retJson.sort(timeCompare);
		
		return retJson;
	}
	
	function json2lyrics(json, type)//type:0.listen,1.fill the blank;no function to chinese song
	{			
		var content = '';
		var lastLrc = "";
		var curLrc = "";
		for(var i = 0; i < json.length; i++)
		{	
			curLrc = json[i].content;
			if(type == 1 && i > 3 && json[i].content != lastLrc)
			{
				var tryTimes = 5;				
				var words = json[i].content.split("&nbsp;");
				var index = Math.floor(Math.random() * words.length);					
				while((words[index].indexOf("&") != -1 || words[index].indexOf("'") != -1 || words[index].length <= 3) && (tryTimes--))
				{
					index = Math.floor(Math.random() * words.length);
				}
				if(tryTimes != 0)
				{
					if(words[index].charAt(words[index].length - 1) == ",")
					{
						words[index] = words[index].substr(0, words[index].length - 1);
						if(index + 1 < words[index].length)
						{
							words[index + 1] = "," + words[index + 1];
						}
						else
						{
							words[index + 1] = ","
						}					
					}
					var width = words[index].length * 10;
					words[index] = '<input class="lrcWord" type="text" value="" style="width:' + width + 'px"/><input value="' + words[index] + '"  class="lrcInfo">';
				}
				curLrc = words.join("&nbsp;");
			}
			lastLrc = json[i].content;
			content += '<span class="lyrics_line">' + curLrc + '</span>';				
		}
		
		return content;
	}	
	
	
	function status(str, type)//type == 0 normal, type == 1 waiting
	{
		$("#status").css("font-weight","normal");
		if(type == null || type == 0)
		{
			$("#statusBar .ui-icon").css("background-image","url(css/images/ui-icons_228ef1_256x240.png)");
			$("#statusBar .ui-icon").css("background-position","-16px -144px");	
		}
		else if(type == 1)
		{
			$("#statusBar .ui-icon").css("background-image","url(css/images/loading.gif)");
			$("#statusBar .ui-icon").css("background-position","0 0");			
		}
		else if(type == 3)
		{
			$("#status").css("font-weight","bold");
		}
		$("#status").html(str);
	}
	
	function log(str)
	{
		//$("#log").append(str + "<br/>");
	}
	
	
	function gotoMusic(delay)
	{
		/*setInterval(delay, (
			$(".audioplayer-playpause").click(),
			$("a[href=#tabs-2]").click(),
			(g_type==1)?$(".lrcWord")[0].focus():0)
			);*/
	}	